package org.mybatis.generator.file;

import org.mybatis.generator.api.GeneratedJavaFile;
import org.mybatis.generator.api.dom.java.CompilationUnit;
import org.mybatis.generator.formatter.TemplateFormatter;
import org.mybatis.generator.internal.util.StringUtility;

import java.util.Map;
import java.util.Properties;

public class GenerateByTemplateFile extends GeneratedJavaFile {

    public static final String ENCODING = "UTF-8";

    private String targetPackage;

    private String fileName;

    private String templateContent;

    private Properties properties;

    private TemplateFormatter templateFormatter;

    private String tableName;

    private Map<String, Object> templateParams;

    public GenerateByTemplateFile(TemplateFormatter templateFormatter, Properties properties, String targetProject, String targetPackage, String fileName, String templateContent) {
        super(null, targetProject, ENCODING, null);
        this.targetProject = targetProject;
        this.targetPackage = targetPackage;
        this.fileName = fileName;
        this.templateContent = templateContent;
        this.properties = properties;
        this.templateFormatter = templateFormatter;
    }

    public GenerateByTemplateFile(String tableName,TemplateFormatter templateFormatter, Properties properties, String targetProject, String targetPackage,String templateContent) {
        super(null, targetProject, ENCODING, null);
        this.tableName = tableName;
        this.targetProject = targetProject;
        this.targetPackage = targetPackage;
        this.fileName = tableName+".java";
        this.templateContent = templateContent;
        this.properties = properties;
        this.templateFormatter = templateFormatter;
    }

    public GenerateByTemplateFile(String tableName,TemplateFormatter templateFormatter, Properties properties, String targetProject, String targetPackage,String templateContent,Map<String, Object> templateParams, String fileName) {
        super(null, targetProject, ENCODING, null);
        this.tableName = tableName;
        this.targetProject = targetProject;
        this.targetPackage = targetPackage;
        this.fileName = fileName;
        this.templateContent = templateContent;
        this.properties = properties;
        this.templateFormatter = templateFormatter;
        this.templateParams = templateParams;
    }

    @Override
    public CompilationUnit getCompilationUnit() {
        return null;
    }

    @Override
    public String getFileName() {
        return fileName;
    }

    @Override
    public String getFormattedContent() {
        if(StringUtility.stringHasValue(tableName) && templateParams != null){
            return templateFormatter.getFormattedContent(tableName,properties, targetPackage, templateContent,templateParams);
        }else if(StringUtility.stringHasValue(tableName)){
            return templateFormatter.getFormattedContent(tableName,properties, targetPackage, templateContent);
        }else {
            return templateFormatter.getFormattedContent(properties, targetPackage, templateContent);
        }
    }

    @Override
    public String getTargetPackage() {
        return targetPackage;
    }

    @Override
    public boolean isMergeable() {
        return false;
    }
}
